package entity;

import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import util.MD5Hash;

/**
 *
 * @author Francis
 */
public class PersonService {

    private static EntityManagerFactory emf = EntityManagerHelper.getFactory();
    public static final Person EMPTY = new Person();
    

    static {
        EMPTY.setActivated(true);
        EMPTY.setAddress("empty");
        EMPTY.setBirthYear(0);
        EMPTY.setBirthMonth(0);
        EMPTY.setEmail("empty@empty.com");
        EMPTY.setFirstname("empty");
        EMPTY.setGender("M");
        EMPTY.setLastname("empty");
        EMPTY.setLoginname("empty");
        EMPTY.setMailPromotion(false);
        EMPTY.setPassword("empty");
        EMPTY.setPersonID(0);
        EMPTY.setRating(0);
        EMPTY.setRegtertime(new Date());
    }

    public static String Login(String loginname, String password) {
        return null;
    }

    public static String getActivationCode(Person p) {
        return MD5Hash.createHash(p.getLoginname(), p.getEmail(), p.getPassword());
    }

    public static boolean activate(String loginname, String password, String inputCode) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNamedQuery("Person.findByLoginname").setParameter("loginname", loginname);
        try {
            Person p = (Person) query.getSingleResult();
            if (p.getActivated()) {
                return true;
            } else if (p.getPassword().equals(password)) {
                String correctCode = getActivationCode(p);
                if (correctCode.equals(inputCode)) {
                    em.getTransaction().begin();
                    p.setActivated(true);
                    em.merge(p);
                    em.getTransaction().commit();
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }
        return false;
    }

    public static boolean isLoginNameExist(String loginname) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNamedQuery("Person.findByLoginname").setParameter("loginname", loginname);
        try {
            Person p = (Person) query.getSingleResult();
            return true;
        } catch (Exception e) {
            //return false;
        } finally {
            em.close();
        }
        return false;
    }

    public static boolean isEmailExist(String email) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNamedQuery("Person.findByEmail").setParameter("email", email);
        try {
            Person p = (Person) query.getSingleResult();
            return true;
        } catch (Exception e) {
            //return false;
        } finally {
            em.close();
        }
        return false;
    }

    public static boolean updatePassword(Integer personID, String oldPassword, String newPassword) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNamedQuery("Person.updatePassword").
                setParameter("personID", personID).
                setParameter("oldPass", oldPassword).
                setParameter("newPass", newPassword);
        try {
            em.getTransaction().begin();
            if (query.executeUpdate() == 1) {
                em.getTransaction().commit();
                return true;
            } else {
                em.getTransaction().rollback();// id not found or password not match
                return false;
            }
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return false;
    }

    public static boolean updateInformation(Integer personID, Integer birth_year, Integer birth_month, String address , String email) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNamedQuery("Person.updateInformation").
                setParameter("personID", personID).
                setParameter("email", email).
                setParameter("birthYear", birth_year).
                setParameter("birthMonth", birth_month).
                setParameter("address", address);
        try {
            em.getTransaction().begin();
            if (query.executeUpdate() == 1) {
                em.getTransaction().commit();
                return true;
            } else {
                em.getTransaction().rollback();// id not found or password not match
                return false;
            }
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return false;
    }

    public static Person login(String loginname, String password) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNamedQuery("Person.login").setParameter("loginname", loginname).setParameter("password", password);
        try {
            Person p = (Person) query.getSingleResult();
            return p;
        } catch (Exception e) {
            //return false;
        } finally {
            em.close();
        }
        return null;
    }

    public static boolean create(Person p) {
        return EntityManagerHelper.persist(p, emf);
    }

    public static Person getById(int id) {
        return PersonService.getById(Integer.valueOf(id));
    }

    public static Person getById(Integer id) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNamedQuery("Person.findByPersonID").setParameter("personID", id);
        try {
            Person p = (Person) query.getSingleResult();
            return p;
        } catch (Exception e) {
            //return false;
        } finally {
            em.close();
        }
        return null;
    }

    public static Person getById(String id) {
        EntityManager em = emf.createEntityManager();
        try {
            int iid = Integer.parseInt(id);
            Query query = em.createNamedQuery("Person.findByPersonID").setParameter("personID", iid);
            Person p = (Person) query.getSingleResult();
            return p;
        } catch (Exception e) {
            //return false;
        } finally {
            em.close();
        }
        return null;
    }

    public static Person getByLoginname(String name) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNamedQuery("Person.findByLoginname").setParameter("loginname", name);
        try {
            Person p = (Person) query.getSingleResult();
            return p;
        } catch (Exception e) {
            //return false;
        } finally {
            em.close();
        }
        return null;
    }

    public static boolean persist(Person object) {
        return EntityManagerHelper.persist(object, emf);
    }
}
